package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.callback.service.ApiCallBackService;
import com.xbongbong.paas.pojo.dto.ApiCallBackRetrySwitchDTO;
import com.xbongbong.paas.pojo.dto.ApiCallBackSetDTO;
import com.xbongbong.paas.pojo.vo.ApiCallBackGetVO;
import com.xbongbong.paas.pojo.vo.ApiCallBackSetVO;
import com.xbongbong.paas.pojo.vo.RetrySwitchVO;
import com.xbongbong.paas.pojo.vo.TokenUpdateVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 李少杰
 * @date 2020/7/7
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/apiCallBack")
public class ApiCallBackController {

    @Resource
    private ApiCallBackService apiCallBackService;

    /**
     * 设置api回调
     * @param apiCallBackSetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid ApiCallBackSetDTO apiCallBackSetDTO, BindingResult br) {
        XbbResponse<ApiCallBackSetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ApiCallBackSetVO apiCallBackSetVO = apiCallBackService.save(apiCallBackSetDTO);
                response = new XbbResponse<>(apiCallBackSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取回调回显
     * @param baseDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<ApiCallBackGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ApiCallBackGetVO apiCallBackSetGetVO = apiCallBackService.get(baseDTO);
                response = new XbbResponse<>(apiCallBackSetGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 回调配置中的token更新
     * @param baseDTO
     * @param br
     * @return
     * 创建时间 2021/9/2 4:02 PM
     * 修改时间 2021/9/2 4:02 PM
     * @author chy
     */
    @RequestMapping(value = "/token/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String apiTokenUpdate(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<TokenUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TokenUpdateVO tokenUpdateVO = apiCallBackService.tokenUpdate(baseDTO);
                response = new XbbResponse<>(tokenUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 回调重发开关
     * @param apiCallBackRetrySwitchDTO
     * @param br
     * @return
     * 创建时间 2021/12/28 2:35 PM
     * 修改时间 2021/12/28 2:35 PM
     * @author chy
     */
    @RequestMapping(value = "/retry/switch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String retrySwitch(@RequestBody @Valid ApiCallBackRetrySwitchDTO apiCallBackRetrySwitchDTO, BindingResult br) {
        XbbResponse<RetrySwitchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RetrySwitchVO retrySwitchVO = apiCallBackService.retrySwitch(apiCallBackRetrySwitchDTO);
                response = new XbbResponse<>(retrySwitchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
